<template>
	<view class="">
		<Header title="教材" :isHome="false" :isWhite='true'></Header>
		<view class="">
			<view class="flex">
				<view class="discipline-detail" :class="{ 'discipline-detail-active': activeIndex === index }"
					v-for="(item,index) in discipline" :key="index" @click="changeTab(index)">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="main">
			<view class="left">
				<view class="left-content" :class="{ 'left-content-active': item.id === currentSelectedIndex }"
					v-for="(item,index) in typeList" :key="index" @click="change(item.id)">
					<view class="">
						{{item.title}}
					</view>
				</view>
			</view>
			<view class="right">
				<view class="" v-for="(item,index) in list" :key="index">
					<view style="display: flex;align-items: center;justify-content: space-between;"
						@click="toggleExpand(item.id)">
						<view class="right-content-title">
							<view class="border">

							</view>
							<view class="">
								{{item.title}}
							</view>
						</view>

						<view class="toggle-button">
							<image src="/static/up.png" mode="" class="icon1" v-if="expandedItems[item.id]"></image>
							<image src="/static/down.png" mode="" class="icon1" v-else></image>
						</view>
					</view>
					<view class="right-main">
						<view class="right-content" v-for="(itm,inx) in getDisplayItems(item.id)" :key="inx"
							@click="openFile(itm.content,itm.eol)">
							<image :src="imgUrl+ itm.cover_image" mode="" class="img"></image>
							<image src="/static/line.png" mode="" class="line"></image>

							<view class="title">
								{{itm.title}}
							</view>
							<view class="content">
								{{itm.desc}}
							</view>
						</view>
					</view>

				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import {
		get,
		img_url
	} from "../../utils/request";
	export default {
		data() {
			return {
				page: 1,
				isRecommendId: 1,
				imgUrl: img_url,
				pages: getCurrentPages(),
				platform: uni.getSystemInfoSync().platform,
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				currentSelectedIndex: 0,
				discipline: [{
						name: '推荐教材',
					},
					{
						name: '学习资料',
					},

				],
				activeIndex: 0,
				typeList: [],
				list: [],
				contentList: [],
				expandedItems: {},
				isClickDisabled: false
			};
		},
		onLoad() {
			this.getArticle()
		},
		methods: {
			toggleExpand(id) {
				this.expandedItems[id] = !this.expandedItems[id];
			},
			getDisplayItems(id) {
				const items = this.contentList[id] || [];
				if (this.expandedItems[id]) {
					return items;
				}
				return items.slice(0, 2);
			},
			getArticle() {
				get('/article/cateList')
					.then(res => {
						this.typeList = res.data
						// this.list = res.data.subjectypelist
						this.currentSelectedIndex = this.typeList[0].id
						const foundItem = this.typeList.find(item => item.id === this.currentSelectedIndex);
						if (foundItem) {
							this.list = foundItem.subjectypeList;
						}
						console.log(this.list);
						this.getArticleList()
					})
					.catch(err => {
						console.error('GET 请求失败:', err);
					});
			},
			getArticleList() {
				for (let i = 0; i < this.list.length; i++) {
					get('/article/articleList?page=' + this.page + '&gtype_id=' + this.currentSelectedIndex +
							'&stype_id=' + this.list[i].id + '&is_recommend=' + this.isRecommendId)
						.then(res => {
							this.$set(this.contentList, this.list[i].id, res.data.list.data);
							// 初始化展开状态为收起
							this.$set(this.expandedItems, this.list[i].id, false);
						})
						.catch(err => {
							console.error('GET 请求失败:', err);
						});
				}
			},
			async goBack() {
				let param = {
					groupId: this.groupId,
				};
				this.pages.length == 1 ?
					uni.switchTab({
						url: "/pages/index/index",
					}) :
					uni.navigateBack({
						delta: 1,
						success: function() {

							if (param.groupId !== "") {
								const pages = getCurrentPages();
								const prevPage = pages[pages.length - 1];
								prevPage.onLoad(param);
							}
						},
					});
			},

			goHome() {
				uni.switchTab({
					url: "/pages/index/index",
				});
				this.saveTabIndex(0);
			},
			customStyle() {
				return {
					"padding-top": `${this.statusBarHeight}px`,
				};
			},
			changeTab(index) {
				console.log(index);
				this.activeIndex = index
				if (this.activeIndex == 0) {
					this.isRecommendId = 1
				} else if (this.activeIndex == 1) {
					this.isRecommendId = 0
				}
				this.getArticleList()
			},
			change(index) {
				this.currentSelectedIndex = index
				const foundItem = this.typeList.find(item => item.id === this.currentSelectedIndex);
				if (foundItem) {
					this.list = foundItem.subjectypeList;
				}
				this.getArticleList()
			},
			openFile(filePath, fileExtension) {
				if (this.isClickDisabled) {
					return; // 如果正在处理点击事件，直接返回
				}
				this.isClickDisabled = true; // 设置为正在处理点击事件
				uni.showLoading()
				switch (fileExtension) {
					case 'jpeg':
					case 'jpg':
						this.openImage(filePath);
						break;
					case 'xls':
					case 'xlsx':
					case 'doc':
					case 'docx':
					case 'pdf':
						this.openDocument(filePath);
						break;
					case 'mp4':
						this.openVideo(filePath);
						break;
					default:
						uni.showToast({
							title: '暂不支持打开该文件类型',
							icon: 'none'
						})
						this.isClickDisabled = false; // 设置为正在处理点击事件
						uni.showLoading()
				}
			},
			openImage(filePath) {
				this.isClickDisabled = false
				uni.hideLoading()
				uni.previewImage({
					urls: [this.imgUrl + filePath],
					current: filePath
				});
			},
			openVideo(filePath) {
				this.isClickDisabled = false
				uni.hideLoading()
				uni.navigateTo({
					url: '/subpages/banner/materialDetail?path=' + filePath
				})
			},
			openDocument(filePath) {
				var that = this
				uni.downloadFile({
					url: this.imgUrl + filePath,
					success: (res) => {
						if (res.statusCode === 200) {
							const tempFilePath = res.tempFilePath;
							uni.openDocument({
								filePath: tempFilePath,
								success: () => {
									console.log('文件打开成功');
								},
								fail: (err) => {
									uni.showToast({
										title: '文件打开失败',
										icon: 'none'
									})
								}
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '文件下载失败',
							icon: 'none'
						})
					},
					complete: () => {
						that.isClickDisabled = false
						uni.hideLoading()
					}
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		justify-content: space-between;

		// margin-top: 100rpx;
		.toggle-button {
			margin-right: 20rpx;

			.icon1 {
				width: 20.45rpx;
				height: 14.98rpx;
			}
		}


		.left {
			.left-content {
				width: 180rpx;
				text-align: center;
				font-family: PingFangSC-Regular;
				font-weight: 400;
				font-size: 24rpx;
				color: #555555;
				padding: 40rpx 0;
			}

			.left-content-active {
				width: 180rpx;
				text-align: center;
				font-family: PingFangSC-SNaNrpxibold;
				font-weight: 600;
				font-size: 24rpx;
				color: #1A1A1A;
				background: url(/static/bianzu.png) no-repeat center center / cover;

				view {
					border-left: 6rpx solid #FFD275;
					height: 40rpx;

				}
			}
		}

		.right {
			width: 100%;
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #fff;

			.right-content-title {
				display: flex;
				align-items: center;
				font-family: PingFangSC-SNaNrpxibold;
				font-weight: 600;
				font-size: 30rpx;
				color: #222222;

				.border {
					width: 8rpx;
					height: 32rpx;
					background: #FFD275;
					border-radius: 4rpx;
					margin-right: 12rpx;
				}
			}

			.right-main {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
			}

			.right-content {
				border: 3rpx solid #222222;
				border-radius: 12rpx;
				padding: 20rpx 16rpx;
				margin: 20rpx 0;

				image {
					display: block;
					margin: 20rpx auto;
				}

				.img {
					width: 215rpx;
					height: 215rpx;
					display: block;
					margin: 0 auto;
				}

				.line {
					width: 215rpx;
					height: 6rpx;
				}

				.title {
					font-family: PingFangSC-SNaNrpxibold;
					font-weight: 600;
					font-size: 24rpx;
					color: #222222;
					margin-bottom: 10rpx;
				}

				.content {
					font-family: PingFangSC-Regular;
					font-weight: 400;
					font-size: 20rpx;
					color: #999999;
					width: 215rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap
				}
			}

		}
	}

	.flex {
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 316rpx;
		height: 64rpx;
		border: 3rpx solid #1A1A1A;
		border-radius: 12rpx;
		margin: 20rpx auto;
		// background-color: #fff;
	}

	.discipline-detail {
		font-family: PingFangSC-SNaNrpxibold;
		font-weight: 600;
		font-size: 26rpx;
		color: #222222;
		text-align: center;
		line-height: 48rpx;
	}

	.discipline-detail-active {
		font-family: PingFangSC-SNaNrpxibold;
		font-weight: 600;
		font-size: 26rpx;
		width: 144rpx;
		height: 48rpx;
		color: #fff;
		background: url(/static/active-bg.png) no-repeat center center / cover;
	}

	.header {
		position: sticky;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 90;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		background-color: #fff;

		.img {


			image {
				width: 255rpx;
				height: 50rpx;
				// background-color: #007FC3;
				display: block;
				margin: 0 auto;
			}
		}

		.header-wrap {
			width: 100%;
			height: 44px;
			position: relative;
			line-height: 44px;
			display: flex;
			align-items: center;
			position: relative;
			justify-content: center;

			.left-secton {
				display: flex;
				align-items: center;
				position: absolute;
				left: 32rpx;

				.back {
					width: 40rpx;
					height: 40rpx;

					image {
						width: 100%;
						height: 100%;
						display: block;
					}
				}

				.home {
					width: 40rpx;
					height: 40rpx;
					margin-left: 28rpx;

					image {
						width: 100%;
						height: 100%;
						display: block;
					}
				}

			}

			.title {
				text-align: center;
				font-weight: 500;
				display: flex;
				align-items: center;
				justify-content: center;
				font-family: PingFangSC-Medium;
				font-weight: 500;
				font-size: 36rpx;
				color: #1A1A1A;

				.title-text {
					max-width: 40%;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					display: flex;
					align-items: center;

					image {
						width: 56rpx;
						height: 40rpx;
						margin-right: 10rpx;
					}

					.title-text-in {
						color: #fa5151;
					}

					.title-text-out {
						color: #00b578;
					}
				}

			}
		}
	}

	.home {
		display: flex;
		align-items: center;
		height: 100%;
		padding: 0 10rpx;
	}
</style>